package banding.com.google.Zxing.view;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.TranslateAnimation;


/*
 * 事件分发可能存在冲突。需要后续修改
 */
public class WipeCardView extends CardView implements AnimationSet.AnimationListener {

    private final AnimationSet animationRight;
    private final AnimationSet animationLeft;
    private float lastX;
    private float lastRawX;
    private WipeAnimationListener wipeListener;

    public WipeCardView(Context context) {
        this(context, null);
    }

    public WipeCardView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WipeCardView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        animationRight = new AnimationSet(context, attrs);
        animationLeft = new AnimationSet(context,attrs);
        TranslateAnimation tRight = new TranslateAnimation(
                TranslateAnimation.RELATIVE_TO_SELF, 0f,
                TranslateAnimation.RELATIVE_TO_SELF, 1f,
                TranslateAnimation.RELATIVE_TO_SELF, 0f,
                TranslateAnimation.RELATIVE_TO_SELF, 0f
        );
        TranslateAnimation tLeft = new TranslateAnimation(
                TranslateAnimation.RELATIVE_TO_SELF, 0f,
                TranslateAnimation.RELATIVE_TO_SELF, -1f,
                TranslateAnimation.RELATIVE_TO_SELF, 0f,
                TranslateAnimation.RELATIVE_TO_SELF, 0f
        );
        AlphaAnimation aAnim = new AlphaAnimation(1f, 0f);
        Interpolator interpolator = new AnticipateInterpolator(0.3f);

        animationRight.addAnimation(tRight);
        animationRight.addAnimation(aAnim);
        animationRight.setInterpolator(interpolator);
        animationRight.setDuration(1000);

        animationLeft.addAnimation(tLeft);
        animationLeft.addAnimation(aAnim);
        animationLeft.setInterpolator(interpolator);
        animationLeft.setDuration(1000);

        animationRight.setAnimationListener(this);
        animationLeft.setAnimationListener(this);
    }

    private void startWipeRightAnim() {
        startAnimation(animationRight);
    }

    private void startWipeLeftAnim() {
        startAnimation(animationLeft);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        Log.d(">>>", ev.getRawX() + "");
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            lastRawX = ev.getRawX();
        } else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
            if (Math.abs(ev.getRawX() - lastRawX) > 100) {
                return true;
            }
        }
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
//
        float x = event.getX();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = x;
                LayoutPointHolder.left = getLeft();
                LayoutPointHolder.right = getRight();
                LayoutPointHolder.bottom = getBottom();
                LayoutPointHolder.top = getTop();
                break;

            case MotionEvent.ACTION_MOVE:
                //计算移动的距离
                float offsetX = x - lastX;
                //调用layout方法来重新放置它的位置
                int l = (int) (getLeft() + offsetX);
                int r = (int) (getRight() + offsetX);

                layout(l, getTop(), r, getBottom());
                break;
            case MotionEvent.ACTION_UP:
                float offX = event.getRawX() - lastRawX;

                if (offX > 100) {
                    startWipeLeftAnim();
//                    upFlag = FLAG_UP_RIGHT;
                } else if (offX < -100) {
                    startWipeRightAnim();
//                    upFlag = FLAG_UP_LEFT;
                } else {
//                    upFlag = FLAG_UP_LIMIT;
                    layout(LayoutPointHolder.left, LayoutPointHolder.top, LayoutPointHolder.right, LayoutPointHolder.bottom);
                    return super.onTouchEvent(event);
                }
//                this.scrollTo(0,0);
                break;
        }
        return true;
    }

    private static class LayoutPointHolder {
        static int left;
        static int right;
        static int top;
        static int bottom;
    }

    @Override
    public void onAnimationStart(Animation animation) {
        if (wipeListener != null) {
            wipeListener.onAnimation(animation, WipeAnimationListener.WIPE_START);
        }
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        layout(LayoutPointHolder.left, LayoutPointHolder.top, LayoutPointHolder.right, LayoutPointHolder.bottom);
        if (wipeListener != null) {
            wipeListener.onAnimation(animation, WipeAnimationListener.WIPE_END);
        }
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
        if (wipeListener != null) {
            wipeListener.onAnimation(animation, WipeAnimationListener.WIPE_REPEAT);
        }
    }

//    public final static int FLAG_UP_RIGHT = 1;
//    public final static int FLAG_UP_LEFT = 2;
//    public final static int FLAG_UP_LIMIT = 0;
//    private int upFlag = FLAG_UP_LIMIT;

    public interface WipeAnimationListener {
        int WIPE_START = 0;
        int WIPE_END = 1;
        int WIPE_REPEAT = 2;

        void onAnimation(Animation animation, int state);
    }

    public void setWipeListener(WipeAnimationListener wipeListener) {
        this.wipeListener = wipeListener;
    }
}
